package buaa.paas.controller;

import buaa.paas.Utils.Response;
import buaa.paas.dao.AppMapper;
import buaa.paas.dao.ImageMapper;
import buaa.paas.dao.UserMapper;
import buaa.paas.kubectl.ApiUtil;
import buaa.paas.model.App;
import buaa.paas.model.Image;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1NamespaceBuilder;
import buaa.paas.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@CrossOrigin(origins = {"http://localhost:8080","null"})
@RestController
public class DataController {
    @Autowired
    AppMapper appMapper;
    @Autowired
    ImageMapper imageMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApiUtil util;

    @PostMapping("/login")
    public Response login(@RequestParam("UserName") String UserName,@RequestParam("Password") String Password){
        User user = userMapper.findUser(UserName);
        TreeMap<String, Integer> result = new TreeMap<>();
        if(user != null){
            if(user.getPassword().equals(Password)){
                result.put("uid", user.getUserID());
                Response res = new Response(1001,result);
                return res;
            }
            else{
                Response res = new Response(500, "密码错误",null);
                return res;
            }
        }
        else {
            Response res = new Response(501, "没有这个user",null);
            return res;
        }
    }

    @PostMapping("/register")
    public Response register(@RequestParam("UserName") String UserName,@RequestParam("Password") String Password){
        User user = userMapper.findUser(UserName);
        if(user != null){
           Response res = new Response(200,null);
           return res;
        }
        else {
            User NewUser = new User();
            NewUser.setName(UserName);
            NewUser.setPassword(Password);
            userMapper.createUser(NewUser);
            NewUser = userMapper.findUser(UserName);
            Map<String,Object> result = new TreeMap<>();
            result.put("uid",NewUser.getUserID());
            Response res = new Response(1001,result);
            return res;
        }
    }

    @GetMapping("/findApp")
    public Response getAppByUID(@RequestParam("UserID") int UserID){
        Map<String,Object> result = new TreeMap<>();
        List<App> apps = appMapper.findApp(UserID);
        List<Object> appInfos = new ArrayList<Object>();
        for (App app : apps) {
            Map<String,Object> appInfo = new TreeMap<>();
            appInfo.put("AppName",app.getName());
            appInfo.put("AppID",app.getAppID());
            appInfos.add(appInfo);
        }
        result.put("Apps",appInfos);
        Response res = new Response(result);
        return res;
    }

    @GetMapping("/findImage")
    public Response getImageByUID(@RequestParam("UserID") int UserID){
        Map<String,Object> result = new TreeMap<>();
        List<Image> images = imageMapper.findImage(UserID);
        List<String> imageNames = new ArrayList<String>();
        for (Image image : images) {
            imageNames.add(image.getName());
        }
        result.put("Images",imageNames);
        Response res = new Response(result);
        return res;
    }

    @PostMapping("/createApp")
    public Response createNewApp(@RequestParam("uid") int uid, @RequestParam("name")String appName){
        App app = new App();
        app.setName(appName);
        app.setUserID(uid);
        appMapper.createApp(app);
        V1NamespaceBuilder builder = new V1NamespaceBuilder();
        builder.withKind("Namespace").withApiVersion("v1").withNewMetadata().withName(uid+"-"+app.getAppID()).endMetadata();
        try{
            util.getCoreApi().createNamespace(builder.build(), null, null, null);
            TreeMap<String, Integer> result = new TreeMap<>();
            result.put("aid", app.getAppID());
            return new Response(1001, result);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    @PostMapping("/deleteApp")
    public Response deleteApp(@RequestParam("uid")int uid, @RequestParam("aid")int aid){
        appMapper.deleteApp(aid);
        return new Response(null);
    }
}
